package com.leo.app;

import javafx.animation.FadeTransition;
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyCodeCombination;
import javafx.scene.input.KeyCombination;
import javafx.scene.input.Mnemonic;
import javafx.scene.layout.GridPane;
import javafx.scene.paint.Color;
import javafx.scene.paint.Paint;
import javafx.scene.text.Font;
import javafx.stage.Stage;
import javafx.util.Duration;
import org.apache.commons.lang3.StringUtils;

/**
 * @ClassName Login
 * @Description
 * @Author wangss
 * @date 2020.05.18 01:11
 * @Version 1.0
 */
public class Login extends Application {

    private TextField nameField;
    private PasswordField passwordField;
    private GridPane gridPane;
    private Button login;
    private Button clear;
    private Label nameError;
    private Label passwordError;
    private Scene scene;

    @Override
    public void start(final Stage primaryStage) throws Exception {

        Label nameLabel = new Label("账号：");

        nameField = new TextField();
        nameField.setUserData("wangss");
        nameField.setTooltip(new Tooltip("请输入用户名"));

        nameError = new Label("账号不能为空");
//        nameError.setTextFill(Paint.valueOf("#FF0000"));
        nameError.setFont(Font.font(16d));
        nameError.setTextFill(Color.RED);
        nameError.setVisible(false);

        Label passwordLabel = new Label("密码：");

        passwordField = new PasswordField();
        passwordField.setUserData("123456");
        passwordField.setTooltip(new Tooltip("请输入密码"));

        passwordError = new Label("密码不能为空");
        passwordError.setTextFill(Paint.valueOf("#FF0000"));
        passwordError.setFont(Font.font(16d));
        passwordError.setVisible(false);

        login = new Button("登录");
        clear = new Button("清除");

        gridPane = new GridPane();
        gridPane.setStyle("-fx-background-color: #07faff");
        gridPane.setAlignment(Pos.CENTER);

        gridPane.add(nameLabel, 0, 0);
        gridPane.add(nameField, 1, 0);
        gridPane.add(nameError, 2, 0);
        gridPane.add(passwordLabel, 0, 1);
        gridPane.add(passwordField, 1, 1);
        gridPane.add(passwordError, 2, 1);
        gridPane.add(clear, 0, 2);
        gridPane.add(login, 1, 2);

        gridPane.setHgap(5d);
        gridPane.setVgap(15d);
        GridPane.setMargin(login, new Insets(0, 0, 0, 150d));


        scene = new Scene(gridPane);
        dealAction(primaryStage);

        primaryStage.setScene(scene);
        primaryStage.setTitle("登录");
        primaryStage.setWidth(500d);
        primaryStage.setHeight(350d);
        primaryStage.setResizable(false);
        primaryStage.show();
    }

    public void dealAction(final Stage primaryStage) {

        nameField.textProperty().addListener(new ChangeListener<String>() {
            public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
                if (StringUtils.isBlank(newValue)) {
                    nameError.setVisible(true);
                } else {
                    nameError.setVisible(false);
                }
            }
        });

        passwordField.textProperty().addListener(new ChangeListener<String>() {
            public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
                if (StringUtils.isBlank(newValue)) {
                    passwordError.setVisible(true);
                } else {
                    passwordError.setVisible(false);
                }
            }
        });

        login.setOnAction(new EventHandler<ActionEvent>() {
            public void handle(ActionEvent event) {
                String name = nameField.getText();
                String password = passwordField.getText();
                if (name == null || "".equals(name)) {
                    nameError.setVisible(true);
                    return;
                }
                if (password == null || "".equals(password)) {
                    passwordError.setVisible(true);
                    return;
                }
                if (nameField.getUserData().equals(name) && passwordField.getUserData().equals(password)) {
                    System.out.println("登录成功");
                    primaryStage.close();
                    LoginSuccess success = new LoginSuccess(name, password);
                } else {
                    System.out.println("登录失败");
                    FadeTransition fade = new FadeTransition();
                    fade.setDuration(Duration.seconds(0.5d));
                    fade.setNode(gridPane);
                    fade.setFromValue(0d);
                    fade.setToValue(1d);
                    fade.play();
                }
            }
        });

        KeyCombination kc = new KeyCodeCombination(KeyCode.ENTER, KeyCombination.ALT_DOWN);
        Mnemonic mc = new Mnemonic(login, kc);
        scene.addMnemonic(mc);

        clear.setOnAction(new EventHandler<ActionEvent>() {
            public void handle(ActionEvent event) {
                nameField.clear();
                passwordField.clear();
            }
        });
    }
}
